package offer.algorithm;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：
 * Author: zhangc
 * Date：2017/7/19 23:23
 */
public class PrintMatrix20 {

    public static List<Integer> printMatrix(int[][] matrix) {
        if (matrix==null || matrix.length == 0)
            return null;
        int rowLength = matrix.length;
        int colLenth = matrix[0].length;
        boolean[][] status = new boolean[ rowLength ][ colLenth ];
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < rowLength; i++) {
            for (int j = 0; j < colLenth; j++) {
                status[ i ][ j ] = false;
            }
        }

        int times = 0;

        PrintStayus dir = PrintStayus.L_R;
        int row_index = 0, col_index = 0;
        int i =0;
        while (times < rowLength) {
            switch (dir) {
                case L_R:
                    for (i=col_index; i < colLenth; i++) {
                        if (status[ row_index ][ i ] == false) {
                            list.add(matrix[ row_index ][ i ]);
                            status[ row_index ][ i ] = true;
                        } else {
                            break;
                        }
                    }
                    col_index = i-1;
                    row_index += 1;
                    dir = PrintStayus.U_D;
                    ++times;
                    break;
                case U_D:
                    for (i = row_index; i < rowLength; i++) {
                        if (status[ i ][ col_index ] == false) {
                            list.add(matrix[ i ][ col_index ]);
                            status[ i ][ col_index ] = true;
                        } else
                            break;
                    }
                    row_index = i -1;
                    col_index -=1;
                    dir = PrintStayus.R_L;
                    break;
                case R_L:
                    for (i = col_index; i >= 0; --i) {
                        if (status[ row_index ][ i ] == false) {
                            list.add(matrix[ row_index ][ i ]);
                            status[ row_index ][ i ] = true;
                        } else
                            break;
                    }
                    col_index = i+1;
                    row_index -= 1;
                    dir = PrintStayus.D_U;
                    ++times;
                    break;
                case D_U:
                    for (i = row_index; i >= 0; --i) {
                        if (status[ i ][ col_index ] == false) {
                            list.add(matrix[ i ][ col_index ]);
                            status[ i ][ col_index ] = true;
                        } else
                            break;
                    }
                    row_index = i +1;
                    col_index += 1;
                    dir = PrintStayus.L_R;
                    break;
                default:
                    break;
            }
        }
        return list;
    }
    enum PrintStayus {
        L_R, U_D, R_L, D_U
    }
}
